﻿using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

namespace CWM.Skinn
{
    public static partial class SkinnContextCommands
    {
        [CtxMenuItem("Renderer/Mesh/Normals/Rotate with Tangents", "Rotate a meshes normals and tangents with the renderers rotation.", 0)]
        public static void RendererNormalsTangentsRotate()
        {
            foreach (var item in SkinnContext.GetContextItems())
            {
                var context = item.GetComponent<Renderer>();
                if (SkinnEx.IsNullOrNotInAScene(context)) continue;
                if (SkinnEx.IsNullOrNotVailid(context)) continue;
                if (SkinnEx.IsNullOrEmpty(context.GetSharedMesh().normals)) continue;
                if (SkinnEx.IsNullOrEmpty(context.GetSharedMesh().tangents)) continue;

                SkinnContext.UndoRecord(context, "Rotate Normals & Tangents");
                var mesh = context.GetSharedMesh().Clone() as Mesh;
                SkinnEx.RotateMesh(mesh, context.transform.localRotation, false, true, true);
                context.SetSharedMesh(mesh);
            }
            SkinnContext.Finished();
        }

        [CtxMenuItem("Renderer/Mesh/Normals/Rotate", "Rotate a meshes normals with the renderers rotation.", 0)]
        public static void RendererNormalsRotate()
        {
            foreach (var item in SkinnContext.GetContextItems())
            {
                var context = item.GetComponent<Renderer>();
                if (SkinnEx.IsNullOrNotInAScene(context)) continue;
                if (SkinnEx.IsNullOrNotVailid(context)) continue;
                if (SkinnEx.IsNullOrEmpty(context.GetSharedMesh().normals)) continue;

                SkinnContext.UndoRecord(context, "Rotate Normals");
                var mesh = context.GetSharedMesh().Clone() as Mesh;
                SkinnEx.RotateMesh(mesh, context.transform.localRotation, false, true, false);
                context.SetSharedMesh(mesh);
            }
            SkinnContext.Finished();
        }

        [CtxMenuItem("Renderer/Mesh/Tangents/Rotate", "Rotate a meshes tangents with the renderers rotation.", 0)]
        public static void RendererTangentsRotate()
        {
            foreach (var item in SkinnContext.GetContextItems())
            {
                var context = item.GetComponent<Renderer>();
                if (SkinnEx.IsNullOrNotInAScene(context)) continue;
                if (SkinnEx.IsNullOrNotVailid(context)) continue;
                if (SkinnEx.IsNullOrEmpty(context.GetSharedMesh().tangents)) continue;

                SkinnContext.UndoRecord(context, "Rotate Tangents");
                var mesh = context.GetSharedMesh().Clone() as Mesh;
                SkinnEx.RotateMesh(mesh, context.transform.localRotation, false, false, true);
                context.SetSharedMesh(mesh);
            }
            SkinnContext.Finished();
        }

    }
}